package com.briup.jz.service.impl;

import java.util.List;

//import javax.validation.constraints.NotNull;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.briup.jz.bean.BaseConfig;
import com.briup.jz.bean.BaseConfigExample;
import com.briup.jz.dao.BaseConfigMapper;
import com.briup.jz.service.IConfigService;
import com.briup.jz.utils.CustomerException;

@Service
public class BaseConfigServiceImpl implements IConfigService {
	
	@Autowired
	private BaseConfigMapper baseconfigMapper;
	
	@Override
	public void saveOrUpdate(BaseConfig baseconfig) throws CustomerException {
		if(baseconfig.getId()!= null) {
			baseconfigMapper.updateByPrimaryKey(baseconfig);
		} else {
			// 判断是否有同名的分类，如果有抛出异常
		    BaseConfigExample example = new BaseConfigExample();
			example.createCriteria().andNameEqualTo(baseconfig.getName());
			List<BaseConfig> list = baseconfigMapper.selectByExample(example);
     		if(list.size()>0) {
				throw new CustomerException("同名的分类已存在");
     		}
			baseconfigMapper.insert(baseconfig);
		}
	}
	
//	@Override
//	public List<BaseConfig> findAll() {
//		BaseConfigExample example = new BaseConfigExample();
//		return baseconfigMapper.selectByExample(example);
//	}
//	
	@Override
	public BaseConfig findByKey(Long id) {
    return baseconfigMapper.selectByPrimaryKey(id);
	}

	@Override
	public List<BaseConfig> query(String name) {
		BaseConfigExample example = new BaseConfigExample();
		if(name!=null){
			example.createCriteria().andNameLike("%"+name+"%");
		}
		List<BaseConfig> list = baseconfigMapper.selectByExample(example);
		return list;
	}

	@Override
	public void deleteById(long id) throws CustomerException {
		BaseConfig baseconfig = baseconfigMapper.selectByPrimaryKey(id);
		if(baseconfig == null) {
			throw new CustomerException("要删除的分类信息不存在");
		}
		baseconfigMapper.deleteByPrimaryKey(id);
		
	}

}
